﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;
using SF.Common;//Please add references
namespace SF.DAL
{
	/// <summary>
	/// 数据访问类:sf_goods_order
	/// </summary>
	public partial class sf_goods_order
	{
		public sf_goods_order()
		{}
		#region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_goods_order");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_goods_order");
            if (where!="")
            {
                strSql.Append(" where " + where);
            }
            return DbHelperSQL.Exists(strSql.ToString());
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(SF.Model.sf_goods_order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sf_goods_order(");
            strSql.Append("busnieseNum,appNum,templatesNum,orderNum,userNum,totalMoney,isPay,isUserDone,isComDone,addTime,payTime,addressNum,addressText,payWay,isExpress,expressName,expressNum,expressTime,comDoneTime)");
			strSql.Append(" values (");
            strSql.Append("@busnieseNum,@appNum,@templatesNum,@orderNum,@userNum,@totalMoney,@isPay,@isUserDone,@isComDone,@addTime,@payTime,@addressNum,@addressText,@payWay,@isExpress,@expressName,@expressNum,@expressTime,@comDoneTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@orderNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@totalMoney", SqlDbType.Decimal,9),
					new SqlParameter("@isPay", SqlDbType.Int,4),
					new SqlParameter("@isUserDone", SqlDbType.Int,4),
					new SqlParameter("@isComDone", SqlDbType.Int,4),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@payTime", SqlDbType.DateTime),
					new SqlParameter("@addressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@addressText", SqlDbType.NVarChar,-1),
					new SqlParameter("@payWay", SqlDbType.Int,4),
					new SqlParameter("@isExpress", SqlDbType.Int,4),
					new SqlParameter("@expressName", SqlDbType.NVarChar,20),
					new SqlParameter("@expressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@expressTime", SqlDbType.DateTime),
					new SqlParameter("@comDoneTime", SqlDbType.DateTime)};
			parameters[0].Value = model.busnieseNum;
			parameters[1].Value = model.appNum;
			parameters[2].Value = model.templatesNum;
			parameters[3].Value = model.orderNum;
			parameters[4].Value = model.userNum;
			parameters[5].Value = model.totalMoney;
			parameters[6].Value = model.isPay;
			parameters[7].Value = model.isUserDone;
			parameters[8].Value = model.isComDone;
			parameters[9].Value = model.addTime;
			parameters[10].Value = model.payTime;
			parameters[11].Value = model.addressNum;
			parameters[12].Value = model.addressText;
            parameters[13].Value = model.payWay;
            parameters[14].Value = model.isExpress;
            parameters[15].Value = model.expressName;
            parameters[16].Value = model.expressNum;
            parameters[17].Value = model.expressTime;
            parameters[18].Value = model.comDoneTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_goods_order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sf_goods_order set ");
			strSql.Append("busnieseNum=@busnieseNum,");
			strSql.Append("appNum=@appNum,");
			strSql.Append("templatesNum=@templatesNum,");
			strSql.Append("orderNum=@orderNum,");
			strSql.Append("userNum=@userNum,");
			strSql.Append("totalMoney=@totalMoney,");
			strSql.Append("isPay=@isPay,");
			strSql.Append("isUserDone=@isUserDone,");
			strSql.Append("isComDone=@isComDone,");
			strSql.Append("addTime=@addTime,");
			strSql.Append("payTime=@payTime,");
			strSql.Append("addressNum=@addressNum,");
			strSql.Append("addressText=@addressText,");
			strSql.Append("payWay=@payWay,");
			strSql.Append("isExpress=@isExpress,");
			strSql.Append("expressName=@expressName,");
			strSql.Append("expressNum=@expressNum,");
            strSql.Append("expressTime=@expressTime,");
            strSql.Append("comDoneTime=@comDoneTime");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@orderNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@totalMoney", SqlDbType.Decimal,9),
					new SqlParameter("@isPay", SqlDbType.Int,4),
					new SqlParameter("@isUserDone", SqlDbType.Int,4),
					new SqlParameter("@isComDone", SqlDbType.Int,4),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@payTime", SqlDbType.DateTime),
					new SqlParameter("@addressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@addressText", SqlDbType.NVarChar,-1),
					new SqlParameter("@payWay", SqlDbType.Int,4),
					new SqlParameter("@isExpress", SqlDbType.Int,4),
					new SqlParameter("@expressName", SqlDbType.NVarChar,20),
					new SqlParameter("@expressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@expressTime", SqlDbType.DateTime),
					new SqlParameter("@comDoneTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.busnieseNum;
			parameters[1].Value = model.appNum;
			parameters[2].Value = model.templatesNum;
			parameters[3].Value = model.orderNum;
			parameters[4].Value = model.userNum;
			parameters[5].Value = model.totalMoney;
			parameters[6].Value = model.isPay;
			parameters[7].Value = model.isUserDone;
			parameters[8].Value = model.isComDone;
			parameters[9].Value = model.addTime;
			parameters[10].Value = model.payTime;
			parameters[11].Value = model.addressNum;
			parameters[12].Value = model.addressText;
            parameters[13].Value = model.payWay;
            parameters[14].Value = model.isExpress;
            parameters[15].Value = model.expressName;
            parameters[16].Value = model.expressNum;
            parameters[17].Value = model.expressTime;
            parameters[18].Value = model.comDoneTime;
			parameters[19].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_goods_order ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_goods_order ");
            if (where!="")
            {
                strSql.Append(" where " + where);
            }
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sf_goods_order ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_goods_order GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalMoney,isPay,isUserDone,isComDone,addTime,payTime,addressNum,addressText,payWay,isExpress,expressName,expressNum,expressTime,comDoneTime from sf_goods_order ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_goods_order GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalMoney,isPay,isUserDone,isComDone,addTime,payTime,addressNum,addressText,payWay,isExpress,expressName,expressNum,expressTime,comDoneTime from sf_goods_order ");
            if (where!="")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_goods_order DataRowToModel(DataRow row)
		{
			SF.Model.sf_goods_order model=new SF.Model.sf_goods_order();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["busnieseNum"]!=null)
				{
					model.busnieseNum=row["busnieseNum"].ToString();
				}
				if(row["appNum"]!=null)
				{
					model.appNum=row["appNum"].ToString();
				}
				if(row["templatesNum"]!=null)
				{
					model.templatesNum=row["templatesNum"].ToString();
				}
				if(row["orderNum"]!=null)
				{
					model.orderNum=row["orderNum"].ToString();
				}
				if(row["userNum"]!=null)
				{
					model.userNum=row["userNum"].ToString();
				}
				if(row["totalMoney"]!=null && row["totalMoney"].ToString()!="")
				{
					model.totalMoney=decimal.Parse(row["totalMoney"].ToString());
				}
				if(row["isPay"]!=null && row["isPay"].ToString()!="")
				{
					model.isPay=int.Parse(row["isPay"].ToString());
				}
				if(row["isUserDone"]!=null && row["isUserDone"].ToString()!="")
				{
					model.isUserDone=int.Parse(row["isUserDone"].ToString());
				}
				if(row["isComDone"]!=null && row["isComDone"].ToString()!="")
				{
					model.isComDone=int.Parse(row["isComDone"].ToString());
				}
				if(row["addTime"]!=null && row["addTime"].ToString()!="")
				{
					model.addTime=DateTime.Parse(row["addTime"].ToString());
				}
				if(row["payTime"]!=null && row["payTime"].ToString()!="")
				{
					model.payTime=DateTime.Parse(row["payTime"].ToString());
				}
				if(row["addressNum"]!=null)
				{
					model.addressNum=row["addressNum"].ToString();
				}
				if(row["addressText"]!=null)
				{
					model.addressText=row["addressText"].ToString();
				}
				if(row["payWay"]!=null && row["payWay"].ToString()!="")
				{
					model.payWay=int.Parse(row["payWay"].ToString());
				}
				if(row["isExpress"]!=null && row["isExpress"].ToString()!="")
				{
					model.isExpress=int.Parse(row["isExpress"].ToString());
				}
				if(row["expressName"]!=null)
				{
					model.expressName=row["expressName"].ToString();
				}
				if(row["expressNum"]!=null)
				{
					model.expressNum=row["expressNum"].ToString();
				}
                if (row["expressTime"] != null && row["expressTime"].ToString() != "")
				{
                    model.expressTime = DateTime.Parse(row["expressTime"].ToString());
				}
                if (row["comDoneTime"] != null && row["comDoneTime"].ToString() != "")
				{
                    model.comDoneTime = DateTime.Parse(row["comDoneTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalMoney,isPay,isUserDone,isComDone,addTime,payTime,addressNum,addressText,payWay,isExpress,expressName,expressNum,expressTime,comDoneTime ");
			strSql.Append(" FROM sf_goods_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalMoney,isPay,isUserDone,isComDone,addTime,payTime,addressNum,addressText,payWay,isExpress,expressName,expressNum,expressTime,comDoneTime ");
			strSql.Append(" FROM sf_goods_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sf_goods_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from sf_goods_order T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}


        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM sf_goods_order");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 计算某个字段的总和值
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public DataSet GetSumField(string field, string where)
        {
            String sql = @"select SUM({0}) as {0} from  sf_goods_order {1}";
            if (String.IsNullOrEmpty(field))
                return null;
            else
            {
                sql = String.Format(sql, field, String.IsNullOrEmpty(where) ? "" : " where " + where);

            }
            return DbHelperSQL.Query(sql);
        }


        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount, string isOur)
        {
            String sql = @"select * from sf_goods_order {0}";
            sql = string.Format(sql, strWhere.Trim() != "" ? " where " + strWhere : "");
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(sql)));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, sql, filedOrder));
        }


        /// <summary>
        /// 修改物流打单
        /// </summary>
        public bool UpdateField(long id, string strValue)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update sf_goods_order set " + strValue);
                strSql.Append(" where id=" + id);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 获得财务结算的订单数据列表
        /// </summary>
        public DataSet GetFinanceOrderList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select o.id,o.busnieseNum,m.real_name,o.appNum,a.appid_name,o.templatesNum,c.settingname,o.orderNum,o.userNum,o.totalMoney,o.isPay,o.isUserDone,o.isComDone,o.addTime,o.payTime,o.addressNum,o.addressText,o.payWay,o.isExpress");
            strSql.Append(" FROM sf_goods_order o ");
            strSql.Append(" left join sf_manager m on o.busnieseNum = m.busnieseNum");
            strSql.Append(" left join sf_appidmanage a on o.appNum = a.appid");
            strSql.Append(" left join sf_modeconfig c on o.templatesNum = c.settingNum");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string strValue, string where)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update sf_goods_order set " + strValue);
                strSql.Append(" where " + where);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }
		#endregion  ExtensionMethod
	}
}

